import Api from '../api';

export async function getSentence({ state, commit }, val) {
    commit('SET_LOAD_STATUS', 'pending');
    let { data } = await this.$axios.post(Api.getSentence, {
        ...state.actionOpt
        // order: 'rand'
    });
    let sentence = data.sentence || [];
    let thumbsUp = data.thumbsUp || [];
    let collect = data.collect || [];
    let collectList = collect.map(item => item.sentence_id);
    let sentenceThumbsUp = thumbsUp.filter(item => item.type === 1).map(it => it.type_id);
    commit('SET_SENTENCE_THUMBSUP', sentenceThumbsUp);
    commit('SET_SENTENCE_COLLECT', collectList);
    if (val.append) {
        commit('APPEND_SENTENCE_LIST', sentence);
        return;
    }
    commit('SET_SENTENCE_LIST', sentence);
}

export async function getSpecial({state, commit}, val) {
    let { data } = await this.$axios.post(Api.getSpecial, val);
    commit('SET_SPECIAL_LIST', data);
}

export async function getContent({state, commit}, params) {
    let { data } = await this.$axios.post(Api.getContent, {
        id: params.id
    });
    let sentence = data.sentence || {};
    let thumbsUp = data.thumbsUp || [];
    let collect = data.collect || [];
    let collectList = collect.map(item => item.sentence_id);
    let sentenceThumbsUp = thumbsUp.filter(item => item.type === 1).map(it => it.type_id);
    let commentList = thumbsUp.filter(item => item.type === 2).map(it => it.type_id);
    commit('SET_SENTENCE_THUMBSUP', sentenceThumbsUp);
    commit('SET_COMMENT_THUMBSUP', commentList);
    commit('SET_SENTENCE_COLLECT', collectList);
    commit('SET_SENTENCE_CONTENT', sentence);
}
// 某个句子分类列表
export async function getSpecialList({state, commit}, params) {
    let categoryList = await this.$axios.post(Api.getSpecialList, params);
    let categoryListData = categoryList.data || [];
    let count = categoryList.count || 0;
    let maxPage = categoryList.maxPage || 0;
    commit('SET_CATEGORY_LIST', categoryListData);
    commit('SET_SPECIAL_COUNT', count);
    commit('SET_SPECIAL_MAXPAGE', maxPage);
}
// 某个句集详情
export async function getSpecialDetails({state, commit}, params) {
    let sentenceList = await this.$axios.post(Api.getSpecialDetails, {
        specialId: params.id
    });
    let sentenceListData = sentenceList.data;
    commit('SET_SPECIAL_DETAILS', sentenceListData);
}
// 搜索
export async function getSearchResult({commit}, params) {
    let searchResult = await this.$axios.post(Api.getSearchResult, params);
    commit('SET_SEARCH_RESULT', searchResult);
}
// 点赞
export async function thumbsUp({ commit }, params) {
    let result = await this.$axios.post(Api.thumbsUp, params);
    if (result.code === 0) {
        let data = result.data || [];
        let sentenceList = data.filter(item => item.type === 1).map(it => it.type_id);
        let commentList = data.filter(item => item.type === 2).map(it => it.type_id);
        commit('SET_SENTENCE_THUMBSUP', sentenceList);
        commit('SET_COMMENT_THUMBSUP', commentList);
        commit('UPDATE_THUMBSUP_NUM', params);
    }
    return result;
}
// 收藏
export async function collect({ commit }, params) {
    let result = await this.$axios.post(Api.collect, params);
    if (result.code === 0) {
        let data = result.data || [];
        let collectList = data.map(it => it.sentence_id);
        commit('UPDATE_COLLECT', { 
            ...params,
            collectList
        });
    }
    return result;
}
// 提交评论
export async function setCommont({ commit }, params) {
    let result =  await this.$axios.post(Api.commont, params);
    if (result.code === 0) {
        console.log(result);
        commit('SET_SENTENCE_COMMONT', result.data || []);
        commit('SET_SENTENCE_COMMONT_COUNT', result.count || 0);
    }
}
// 拉取评论
export async function getCommont({ commit }, params) {
    let result =  await this.$axios.post(Api.getCommont, params);
    if (result.code === 0) {
        console.log(result);
        commit('SET_SENTENCE_COMMONT', result.data || []);
        commit('SET_SENTENCE_COMMONT_COUNT', result.count || 0);
    }
}
// 评论点赞
export async function commentPraise({ dispatch }, params) {
    return await dispatch('thumbsUp', params);
}
// 评论回复
export async function handReplyCommont({ commit }, params) {
    await this.$axios.post(Api.replyCommont, params);
}
// 我的收藏列表
export async function getCollectList({ commit }, params) {
   let { code, data } = await this.$axios.post(Api.getCollectList, params);
   if (code === 0) {
        commit('SET_COLLECT_LIST', data.data || []);
        commit('SET_COLLECT_COUNT', data.count || 0);
        commit('SET_COLLECT_TOTALPAGE', data.totalPage || 0);
   }
}
// 我的点赞列表
export async function getPraiseList({ commit }, params) {
   let { code, data} = await this.$axios.post(Api.getPraiseList, params);
   if (code === 0) {
        commit('SET_PRAISE_LIST', data.data || []);
        commit('SET_PRAISE_COUNT', data.count || 0);
        commit('SET_PRAISE_TOTALPAGE', data.totalPage || 0);
   }
}
